<template>
  <div class="password">
    <el-form
      ref="passwordForm"
      :model="passwordForm"
      :rules="passwordRules"
      class="password-form"
    >
      <h1 class="title">忘记密码</h1>
      <el-form-item prop="phone">
        <el-input
          v-model="passwordForm.username"
          type="text"
          auto-complete="off"
          placeholder="请输入手机号"
        >
        </el-input>
      </el-form-item>
      <div class="code">
        <el-form-item prop="code">
          <el-input
            v-model="passwordForm.code"
            type="text"
            auto-complete="off"
            placeholder="请输入验证码"
          >
          </el-input>
        </el-form-item>
        <img :src="imgSrc" @click="refresh" />
      </div>
      <el-form-item prop="password">
        <el-input
          v-model="passwordForm.password"
          type="password"
          auto-complete="off"
          placeholder="请输入新密码"
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="newPassword">
        <el-input
          v-model="passwordForm.newPassword"
          type="password"
          auto-complete="off"
          placeholder="请确认新密码"
        >
        </el-input>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width: 100%"
          @click="submitForm()"
        >
          <span v-if="!loading">重 置 密 码</span>
          <span v-else>重 置 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getCodeImg, forgetPassword } from "@/api/login";

export default {
  name: "password",
  data() {
    return {
      passwordForm: {
        username: "",
        code: "",
        password: "",
        newPassword: "",
        uuid: "",
      },
      passwordRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        code: [{ required: true, trigger: "blur", message: "请输入验证码" }],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        newPassword: [
          { required: true, trigger: "blur", message: "请确认您的密码" },
        ],
      },
      loading: false,
      imgSrc: "",
    };
  },
  methods: {
    submitForm() {
      this.$refs["passwordForm"].validate((valid) => {
        if (valid) {
          if (this.passwordForm.password != this.passwordForm.newPassword) {
            this.$modal.msgError("两次密码输入不一致！");
            this.getImg();
          } else {
            this.loading = true;
            this.forget();
            this.loading = false;
          }
        } else {
          return false;
        }
      });
    },
    async getImg() {
      await getCodeImg().then((res) => {
        this.imgSrc = res.img;
        this.passwordForm.uuid = res.uuid;
      });
    },
    refresh() {
      this.getImg();
    },
    forget() {
      delete this.passwordForm.newPassword;
      forgetPassword(this.passwordForm).then((res) => {
        this.$modal.msgSuccess("修改成功！");
        this.$router.push("/login");
      });
    },
  },
  created() {
    this.getImg();
  },
};
</script>

<style lang="scss" scoped>
.password {
  padding-right: 7%;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  width: 100%;
  height: 100%;
  background-color: rgba(245, 248, 255, 0.7);
  background-image: url("../assets/images/bg.png");
  background-size: cover;
}

.password-form {
  border-radius: 24px;
  background: #ffffff;
  width: 616px;
  padding: 71px 64px 75px 64px;

  .title {
    margin: 0px auto 30px auto;
    font-size: 48px;
    color: #06080c;
  }

  .code {
    display: flex;
    justify-content: space-between;

    .el-input {
      width: 296px;

      &::v-deep .el-input__inner {
        width: 100%;
      }
    }

    img {
      width: 168px;
      height: 72px;
      border-radius: 8px;
    }
  }

  .el-form-item {
    margin-bottom: 32px;

    .el-input {
      height: 72px;
      border-radius: 8px;

      &::v-deep input {
        padding-left: 36px;
        height: 72px;
        font-size: 24px;
      }
    }

    &::v-deep .el-form-item__error {
      font-size: 16px;
    }

    .el-button {
      width: 488px;
      height: 72px;
      font-size: 30px;
      border-radius: 36px;
    }
  }
}
</style>
